<?php


	namespace app\admin\controller\member;


	use core\basic\Controller;
	use app\admin\model\member\ActivationModel;

	class ActivationController extends Controller
	{

		private $model;

		public function __construct()
		{
			$this->model = new ActivationModel();
		}

		public function index()
		{

			if ((!!$id = get('id', 'int')) && $result = $this->model->getMember($id)) {
				$this->assign('more', true);
				$this->assign('fields', $this->model->getFields());
				$this->assign('member', $result);

			} else {
				$this->assign('list', true);
				if (!!($field = get('field', 'var')) && !!($keyword = get('keyword', 'vars'))) {
					$result = $this->model->findCode($field, $keyword);
				} else {
					$result = $this->model->getList();
				}

				$this->assign('ActCode', $result);
			}
			$this->display('member/Activation.html');
		}


		public function add()
		{
			if ($_POST) {

				$number = post('number');
				$usermobile = post('usermobile');

				if (!$userInof=model('admin.member.Member')->checkMember(['usermobile' => $usermobile])) {
					error('当前手机号码不存！', -1);
				}
				if($number){
					for ($i = 0; $i < $number; $i++) {
						$check = true;
						while ($check == true) {
							$cdkey = makeCardPassword();
							$result = $this->model->checkCodefind(['code' => $cdkey]);
							if (empty($result)) {
								$data['code'] = $cdkey;
								$data['status'] = 1;
								$data['add_time'] = get_datetime();
								$data['source'] = '后台派送';
								$data['ues_time'] = '';
								$data['user_id'] = '';
								$data['usermobile'] = $usermobile;
								$result = $this->model->addActivation($data);
								$check = false;

							}
						}
					}

				}
				if ($result) {
					$this->log($usermobile.'新增'.$number.'激活码成功！');
					model('admin.member.Bill')->AddBill($userInof->id, 3, '获取后台派发的' . $number.'条激活码',4);
					success('新增成功！', url('/admin/Activation/index'));
				} else {
					$this->log('新增会员失败！');
					error('新增失败！', -1);
				}

			}
		}



		public function del()
		{
			// 执行批量删除
			if ($_POST) {
				if (!!$list = post('list')) {
					if ($this->model->delActivationList($list)) {
						$this->log('批量删除激活码成功！');
						success('删除成功！', -1);
					} else {
						$this->log('批量删除激活码失败！');
						error('删除失败！', -1);
					}
				} else {
					alert_back('请选择要删除的激活码！');
				}
			}

			if (!$id = get('id', 'int')) {
				error('传递的参数值错误！', -1);
			}

			if ($this->model->delActivation($id)) {
				$this->log('删除激活码' . $id . '成功！');
				success('删除成功！', -1);
			} else {
				$this->log('删除激活码' . $id . '失败！');
				error('删除失败！', -1);
			}
		}


	}